{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a096a0ce-4345-4c92-a536-87d235fd18c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import org.apache.iceberg.catalog.Catalog;\n",
    "import org.apache.hadoop.conf.Configuration;\n",
    "import org.apache.iceberg.CatalogProperties;\n",
    "import org.apache.iceberg.rest.RESTCatalog;\n",
    "import org.apache.iceberg.aws.s3.S3FileIOProperties;\n",
    "\n",
    "\n",
    "Map<String, String> properties = new HashMap<>();\n",
    "\n",
    "properties.put(CatalogProperties.CATALOG_IMPL, \"org.apache.iceberg.rest.RESTCatalog\");\n",
    "properties.put(CatalogProperties.URI, \"http://iceberg-rest:8181\");\n",
    "properties.put(CatalogProperties.WAREHOUSE_LOCATION, \"s3a://warehouse/wh\");\n",
    "properties.put(CatalogProperties.FILE_IO_IMPL, \"org.apache.iceberg.aws.s3.S3FileIO\");\n",
    "properties.put(S3FileIOProperties.ENDPOINT, \"http://minio.net:9000\");\n",
    "\n",
    "RESTCatalog catalog = new RESTCatalog();\n",
    "Configuration conf = new Configuration();\n",
    "catalog.setConf(conf);\n",
    "catalog.initialize(\"demo\", properties);\n",
    "// catalog.name();\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2585389b-f519-4a48-af0a-451019622ae7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "table name: demo.t_rule_action.t_rule_action\n"
     ]
    }
   ],
   "source": [
    "import org.apache.iceberg.Table;\n",
    "import org.apache.iceberg.TableScan;\n",
    "import org.apache.iceberg.catalog.Namespace;\n",
    "import org.apache.iceberg.catalog.TableIdentifier;\n",
    "\n",
    "Namespace ns = Namespace.of(\"t_rule_action\");\n",
    "TableIdentifier table_name = TableIdentifier.of(ns, \"t_rule_action\");\n",
    "Table table = catalog.loadTable(table_name);\n",
    "\n",
    "System.out.println(String.format(\"table name: %s\", table.name()));\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e45fc0c9-c20f-4b7c-a7db-17725d2c9fbf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Record(Mr, Nah, 912381, 4321, Mouse, mouse@email.com)\n",
      "Record(Mr2, Nah2, 912381999, 11849, Mouse2, mouse2@email.com)\n"
     ]
    }
   ],
   "source": [
    "import org.apache.iceberg.io.CloseableIterable;\n",
    "import org.apache.iceberg.data.Record;\n",
    "import org.apache.iceberg.data.IcebergGenerics;\n",
    "\n",
    "table.refresh();\n",
    "\n",
    "CloseableIterable<Record> result = IcebergGenerics.read(table).build();\n",
    "\n",
    "for (Record r: result) {\n",
    "    System.out.println(r);\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a3c552fb-7b93-4bab-8af8-0f29339a1ad4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "GenericDataFile{content=data, file_path=s3://warehouse/t_rule_action/t_rule_action/data/data-file.avro, file_format=AVRO, spec_id=0, partition=PartitionData{}, record_count=2, file_size_in_bytes=2388, column_sizes=null, value_counts=null, null_value_counts=null, nan_value_counts=null, lower_bounds=null, upper_bounds=null, key_metadata=null, split_offsets=null, equality_ids=null, sort_order_id=null, data_sequence_number=6, file_sequence_number=6, referenced_data_file=null, content_offset=null, content_size_in_bytes=null}\n",
      "134: content: optional int (Contents of the file: 0=data, 1=position deletes, 2=equality deletes)\n"
     ]
    }
   ],
   "source": [
    "import org.apache.iceberg.TableScan;\n",
    "import org.apache.iceberg.DataFile;\n",
    "import org.apache.iceberg.CombinedScanTask;\n",
    "\n",
    "\n",
    "TableScan scan = table.newScan();\n",
    "Iterable<CombinedScanTask> result = scan.select(\"*\").planTasks();\n",
    "CombinedScanTask task = result.iterator().next();\n",
    "DataFile dataFile = task.files().iterator().next().file();\n",
    "System.out.println(dataFile);\n",
    "System.out.println(dataFile.CONTENT);\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "f2d9da2c-21c4-46bf-8492-470db0725cd8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[BaseSnapshot{id=697711801, timestamp_ms=1743511509694, operation=append, summary={}, manifest-list=s3://warehouse/t_rule_action/t_rule_action/metadata/snap.avro, schema-id=0}, BaseSnapshot{id=3358094239, timestamp_ms=1743511655812, operation=append, summary={}, manifest-list=s3://warehouse/t_rule_action/t_rule_action/metadata/snap.avro, schema-id=0}]\n",
      "BaseSnapshot{id=3358094239, timestamp_ms=1743511655812, operation=append, summary={}, manifest-list=s3://warehouse/t_rule_action/t_rule_action/metadata/snap.avro, schema-id=0}\n",
      "s3://warehouse/t_rule_action/t_rule_action/metadata/snap.avro\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[GenericManifestFile{content=DATA, path=s3://warehouse/t_rule_action/t_rule_action/metadata/manifest-entry.avro, length=4708, partition_spec_id=0, added_snapshot_id=3358094239, added_data_files_count=1, added_rows_count=2, existing_data_files_count=0, existing_rows_count=0, deleted_data_files_count=0, deleted_rows_count=0, partitions=[], key_metadata=null, sequence_number=6, min_sequence_number=6}]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table.refresh();\n",
    "\n",
    "System.out.println(table.snapshots());\n",
    "\n",
    "System.out.println(table.currentSnapshot());\n",
    "\n",
    "System.out.println(table.currentSnapshot().manifestListLocation());\n",
    "\n",
    "table.io();\n",
    "\n",
    "table.currentSnapshot().allManifests(table.io())\n",
    "\n",
    "/*\n",
    "import org.apache.iceberg.avro.Avro;\n",
    "import org.apache.iceberg.io.InputFile;\n",
    "import org.apache.iceberg.GenericManifestFile;\n",
    "import org.apache.iceberg.ManifestFile;\n",
    "import java.util.List;\n",
    "import org.apache.iceberg.relocated.com.google.common.collect.Lists;\n",
    "*/\n",
    "\n",
    "//InputFile deleteme = table.io().newInputFile(\"s3://warehouse/snap-2104694741369517975-0-742aa2a7-fb0f-411b-9875-c63058bfbc4e.avro\");\n",
    "//InputFile deleteme = table.io().newInputFile(\"s3://warehouse/snap-patch.avro\");\n",
    "//List<ManifestFile> l = Lists.newLinkedList(Avro.read(deleteme).project(ManifestFile.schema()).build());\n",
    "\n",
    "//System.out.println(l);\n",
    "\n",
    "/*\n",
    "InputFile manifest_list = table.io().newInputFile(table.currentSnapshot().manifestListLocation());\n",
    "for (ManifestFile f : Avro.read(manifest_list).project(ManifestFile.schema()).build()) {\n",
    "  System.out.println(f);\n",
    "}\n",
    "*/\n",
    "// List<ManifestFile> l = Lists.newLinkedList(Avro.read(manifest_list).project(ManifestFile.schema()).build());\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cf718805-3e86-459c-aa3c-cb15814cd837",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "table {\n",
       "  1: c_salutation: optional string\n",
       "  2: c_preferred_cust_flag: optional string\n",
       "  3: c_first_sales_date_sk: optional int\n",
       "  4: c_customer_sk: optional int\n",
       "  5: c_first_name: optional string\n",
       "  6: c_email_address: optional string\n",
       "}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table.schema()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Java",
   "language": "java",
   "name": "java"
  },
  "language_info": {
   "codemirror_mode": "java",
   "file_extension": ".jshell",
   "mimetype": "text/x-java-source",
   "name": "Java",
   "pygments_lexer": "java",
   "version": "17.0.14+7-Debian-1deb11u1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
